package com.mini.gateway.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mini.gateway.web.controller.vo.ApiLogPageRequest;
import com.mini.gateway.web.domain.LayTableResult;
import com.mini.gateway.core.modules.log.dto.ApiLogDTO;
import com.mini.gateway.core.modules.log.service.ApiLogService;
import com.mini.gateway.core.modules.route.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * PageController
 *
 */
@Controller
@RequestMapping("/gateway/apilog")
public class ApiLogController {

    @Autowired
    private RouteService routeService;
    @Autowired
    private ApiLogService apiLogService;

    @GetMapping("/page/list")
    public Mono<String> toListPage(final Model model) {
        List<String> serviceList = routeService.getRouteNameList();
        model.addAttribute("serviceList", serviceList);
        return Mono.create(monoSink -> monoSink.success("log/apilog_list"));
    }

    @PostMapping(value = "/data/list")
    @ResponseBody
    public Mono<LayTableResult<ApiLogDTO>> listData(@RequestBody ApiLogPageRequest request) {
        Page<ApiLogDTO> page = apiLogService.getPage(request.getPage(), request.getRouteId(), request.getType(), request.getResponseCode(), request.getStartTime(), request.getEndTime());
        return Mono.just(LayTableResult.success(page));
    }
}
